home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1998 July / EnigmA AMIGA RUN 29 (1998)(G.R. Edizioni)(IT)[!][issue 1998-07 & 08].iso / earcd / phase5 / ppcrelease / examples / msg2.c < prev    next >
C/C++ Source or Header  |  1998-02-21  |  8KB  |  230 lines

  1. /* Msg2 and Msg2PPC.elf
  2.  * 
  3.  * show how to send a Msg from the M68k to the PPC and from the PPC to the M68k.
  4.  * It also shows how to use the StartupMsg and PPCTask`s msgport functions to
  5.  * simplify the init and close phase.
  6.  *
  7.  */
  8. #include <exec/types.h>
  9. #include <exec/nodes.h>
  10. #include <exec/lists.h>
  11. #include <exec/memory.h>
  12. #include <utility/tagitem.h>
  13. #include <powerup/ppclib/interface.h>
  14. #include <powerup/ppclib/message.h>
  15. #include <powerup/ppclib/tasks.h>
  16. #include <powerup/proto/ppc.h>
  17. #include <proto/exec.h>
  18. #include <proto/dos.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include "time.h"
  22. #include "time_protos.h"
  23.  
  24. #define TEXT            "Text sent by M68k processor\n"
  25.  
  26. struct StartupData
  27. {
  28.     void    *MsgPort;
  29. };
  30.  
  31. extern struct Library    *SysBase;
  32.  
  33. int    main(void)
  34. {
  35. struct Library        *PPCLibBase;
  36. struct TagItem        MyTags[10];
  37. void            *PPCPort;
  38. void            *M68kPort;
  39. void            *ReplyPort;
  40. void            *StartupMsg;
  41. void            *M68kMsg;
  42. void            *PPCMsg;
  43. void            *ElfObject;
  44. void            *Task;
  45. UBYTE            *Body;
  46. struct StartupData    *StartupData;
  47. void            *MyTimerObject;
  48.  
  49.   printf("Opening ppc.library\n");
  50.   if (PPCLibBase = OpenLibrary("ppc.library", 44))
  51.   {
  52.     if (MyTimerObject=TimerCreateObject())
  53.     {
  54.       printf("Loading PPC object\n");
  55.       if (ElfObject=PPCLoadObject("PROGDIR:Msg2PPC.elf"))
  56.       {
  57.         printf("Creating reply port\n");
  58.         MyTags[0].ti_Tag    =    TAG_DONE;
  59.         if (ReplyPort = PPCCreatePort(MyTags))
  60.         {
  61.           printf("Creating M68k port\n");
  62.           if (M68kPort = PPCCreatePort(MyTags))
  63.           {
  64.             if (StartupMsg = PPCCreateMessage(ReplyPort, sizeof(struct StartupData)))
  65.             {
  66.               printf("Allocating StartupData\n");
  67.               if (StartupData = PPCAllocVec(sizeof(struct StartupData), MEMF_ANY))
  68.               {
  69.                 StartupData->MsgPort    =    M68kPort;
  70.  
  71.                 printf("Creating PPC task\n");
  72.                 MyTags[0].ti_Tag    =    PPCTASKTAG_STARTUP_MSG;
  73.                 MyTags[0].ti_Data    =(ULONG) StartupMsg;
  74.  
  75.                 MyTags[1].ti_Tag    =    PPCTASKTAG_STARTUP_MSGDATA;
  76.                 MyTags[1].ti_Data    =(ULONG) StartupData;
  77.  
  78.                 MyTags[2].ti_Tag    =    PPCTASKTAG_STARTUP_MSGLENGTH;
  79.                 MyTags[2].ti_Data    =    sizeof(StartupData);
  80.  
  81.                 MyTags[3].ti_Tag    =    PPCTASKTAG_STARTUP_MSGID;
  82.                 MyTags[3].ti_Data    =    0;
  83.  
  84.                 MyTags[4].ti_Tag    =    PPCTASKTAG_MSGPORT;
  85.                 MyTags[4].ti_Data    =    TRUE;
  86.  
  87.                 MyTags[5].ti_Tag    =    TAG_DONE;
  88.  
  89.                 if (Task = PPCCreateTask(ElfObject, MyTags))
  90.                 {
  91.  
  92.                   if (PPCPort=(void*) PPCGetTaskAttrsTags(Task,
  93.                                                           PPCTASKINFOTAG_MSGPORT,0,
  94.                                                           TAG_END))
  95.                   {
  96.                     printf("Allocating memory for message body\n");
  97.                     if (Body = PPCAllocVec(sizeof(TEXT), MEMF_PUBLIC))
  98.                     {
  99.                       printf("Creating message...");
  100.                       TimerSetAttr(MyTimerObject,TIMERTAG_START);
  101.                       if (PPCMsg = PPCCreateMessage(ReplyPort, sizeof(TEXT)))
  102.                       {
  103.                         TimerSetAttr(MyTimerObject,TIMERTAG_STOP);
  104.                         TimerShow(MyTimerObject);
  105.  
  106.                         printf("Sending message...");
  107.                         strcpy(Body, TEXT);
  108.                         TimerSetAttr(MyTimerObject,TIMERTAG_START);
  109.                         PPCSendMessage(PPCPort,
  110.                                        PPCMsg,
  111.                                        Body,
  112.                                        sizeof(TEXT),
  113.                                        0x12345678);
  114.                         TimerSetAttr(MyTimerObject,TIMERTAG_STOP);
  115.                         TimerShow(MyTimerObject);
  116.  
  117.                         printf("Waiting for reply...");
  118.                         TimerSetAttr(MyTimerObject,TIMERTAG_START);
  119.                         PPCWaitPort(ReplyPort);
  120.                         TimerSetAttr(MyTimerObject,TIMERTAG_STOP);
  121.                         TimerShow(MyTimerObject);
  122.  
  123.                         printf("Waiting for PPC message...");
  124.                         TimerSetAttr(MyTimerObject,TIMERTAG_START);
  125.                         PPCWaitPort(M68kPort);
  126.                         TimerSetAttr(MyTimerObject,TIMERTAG_STOP);
  127.                         TimerShow(MyTimerObject);
  128.  
  129.                         printf("Getting message...");
  130.                         TimerSetAttr(MyTimerObject,TIMERTAG_START);
  131.                         if (M68kMsg = PPCGetMessage(M68kPort))
  132.                         {
  133.                           TimerSetAttr(MyTimerObject,TIMERTAG_STOP);
  134.                           TimerShow(MyTimerObject);
  135.                           Printf("Message: %s", (char *) PPCGetMessageAttr(M68kMsg, PPCMSGTAG_DATA));
  136.                           printf("Reply message...");
  137.                           TimerSetAttr(MyTimerObject,TIMERTAG_START);
  138.                           PPCReplyMessage(M68kMsg);
  139.                           TimerSetAttr(MyTimerObject,TIMERTAG_STOP);
  140.                           TimerShow(MyTimerObject);
  141.                         }
  142.                         else
  143.                         {
  144.                           Printf("Did not get PPC msg\n");
  145.                         }
  146.  
  147.                         printf("Waiting for Task Finish Msg...\n");
  148.                         for (;;)
  149.                         {
  150.                           if ((M68kMsg=PPCGetMessage(ReplyPort)) == StartupMsg)
  151.                           {
  152.                             printf("Ahh..the expected Startup=Finish Msg was received.\nNow we can savely free all resources.\n");
  153.                             break;
  154.                           }
  155.                           else
  156.                           {
  157.                             printf("Some non replied Msg 0x%lx was found..wait\n",
  158.                                    M68kMsg);
  159.                             PPCWaitPort(ReplyPort);
  160.                           }
  161.                         }
  162.  
  163.                         printf("Deleting message...");
  164.                         TimerSetAttr(MyTimerObject,TIMERTAG_START);
  165.                         PPCDeleteMessage(PPCMsg);
  166.                         TimerSetAttr(MyTimerObject,TIMERTAG_STOP);
  167.                         TimerShow(MyTimerObject);
  168.                       }
  169.                       else
  170.                       {
  171.                         Printf("Could not create message\n");
  172.                       }
  173.                     }
  174.                     else
  175.                     {
  176.                       Printf("Could not alloc mem for msg body\n");
  177.                     }
  178.                     printf("Freeing message body memory\n");
  179.                     PPCFreeVec(Body); 
  180.                   }
  181.                   else
  182.                   {
  183.                     Printf("Could not find the PPCTask's msgport\n");
  184.                   }
  185.                 }
  186.                 else
  187.                 {
  188.                   Printf("Could not create PPC task\n");
  189.                 }
  190.                 PPCFreeVec(StartupData);
  191.               }
  192.               else
  193.               {
  194.                 Printf("Could not alloc Startup Data\n");
  195.               }
  196.               PPCDeleteMessage(StartupMsg);
  197.             }
  198.             else
  199.             {
  200.               Printf("Could not create Startup message\n");
  201.             }
  202.             printf("Deleting m68k msg port...");
  203.             PPCDeletePort(M68kPort);
  204.           }
  205.           else
  206.           {
  207.             Printf("Could not create m68k msg port\n");
  208.           }
  209.           printf("Deleting m68k reply port...");
  210.           PPCDeletePort(ReplyPort);
  211.         }
  212.         printf("Unloading PPC object\n");
  213.         PPCUnLoadObject(ElfObject);
  214.       }
  215.       else
  216.       {
  217.         Printf("Could not load the elfobject\n");
  218.       }
  219.       TimerDeleteObject(MyTimerObject);
  220.     }
  221.     printf("Closing ppc.library\n");
  222.     CloseLibrary(PPCLibBase);
  223.   }
  224.   else
  225.   {
  226.     Printf("Could not open ppc.library v44+\n");
  227.   }
  228. }
  229.  
  230.